﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using eTruck.Helpers;

namespace eTruck.Views.Users
{
    public partial class Login : System.Web.UI.Page
    {
        string url = "../../Index.aspx";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //检查是否已经有cookies
                if (CookiesHelper.GetCookie(CommonHelper.LoginCookieName) != null)
                {
                    MessageBoxHelper.MessageBox(Page, "", url);
                }
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            string UserName = txtUserName.Text.Trim();
            string Password = txtPassword.Text.Trim();
            int UserID = -1;
            BLL.Users bll = new BLL.Users();
            UserID = bll.Login(UserName, Password);
            Session["UserName"] = "";
            if (UserID == -1)
            {
                //登录失败
                txtUserName.Text = "";
                txtPassword.Text = "";
                MessageBoxHelper.MessageBox(Page, "用户名密码错误！", "#");
                txtUserName.Focus();
            }
            else
            {
                //跳转到主页面
                CookiesHelper.SaveCookie(CommonHelper.LoginCookieName, UserID.ToString(), CommonHelper.LoginCookieHours);
                Session["UserName"] =  txtUserName.Text;
                Response.Redirect(url);
                //MessageBoxHelper.MessageBox(Page, "登录成功！", url);
            }
        }
    }
}